package com.lottery.material.service.portal.user.impl;

import com.lottery.material.dao.article.ArticleInfoMapper;
import com.lottery.material.dao.portaluser.FollowInfoMapper;
import com.lottery.material.dao.portaluser.PortalUserInfoMapper;
import com.lottery.material.pojo.page.Page;
import com.lottery.material.pojo.page.PageData;
import com.lottery.material.pojo.portaluser.FollowInfo;
import com.lottery.material.pojo.portaluser.PortalUserInfo;
import com.lottery.material.service.portal.user.FollowInfoService;
import com.lottery.material.service.portal.user.PortalUserInfoService;
import com.lottery.material.util.Const;
import com.lottery.material.vo.portal.user.FollowUserVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * Description:    用户关注服务实现类
 * Author:         sunny
 * CreateDate:     2019-04-15 14:40
 */
@Service
@Slf4j
public class FollowInfoServiceImpl implements FollowInfoService {

    @Autowired
    private FollowInfoMapper followInfoMapper;

    @Autowired
    private PortalUserInfoMapper portalUserInfoMapper;
    @Autowired
    private ArticleInfoMapper articleInfoMapper;


    /**
     * description: 删除用户关注记录
     * author sunny
     * createDate 2019-04-15 下午 2:41
     * @param followInfo
     */
    @Override
    public Integer deleteUserFollow(FollowInfo followInfo) throws Exception {
        FollowInfo follow = followInfoMapper.findUserFollow(followInfo);
        if(Objects.isNull(follow)){
            log.info("当前用户：{}-->还没有关注该用户{}，无法取消关注",followInfo.getUserId(), followInfo.getFollowUserId());
            throw new Exception("未能查询到关注记录，所以无法取消关注！");
        }

        Integer count = followInfoMapper.deleteByPrimaryKey(follow.getId());
        if(count > 0) {
            portalUserInfoMapper.updateFollowTotal(followInfo.getUserId(), "1"); //用户关注数减1
            portalUserInfoMapper.updateFansTotal(followInfo.getFollowUserId(), "1"); //被关注的用户粉丝数减1
            PortalUserInfo userInfo = portalUserInfoMapper.findByUserId(followInfo.getFollowUserId());
            return userInfo.getFansTotal();
        }else{
            throw new Exception("删除关注记录失败！");
        }
    }

    /**
     * description: 保存用户关注记录
     * author sunny
     * createDate 2019-04-15 下午 2:59
     * @param followInfo
     */
    @Override
    public Integer saveUserFollow(FollowInfo followInfo) throws Exception {
        FollowInfo follow = followInfoMapper.findUserFollow(followInfo);
        if(Objects.nonNull(follow)){
            log.info("当前用户：{}-->已经关注该用户{}，无法再次关注",followInfo.getUserId(), followInfo.getFollowUserId());
            throw new Exception("查询到已经存在关注记录，所以无法再次关注！");
        }
        Integer count = followInfoMapper.insertSelective(followInfo);
        if(count > 0) {
            portalUserInfoMapper.updateFollowTotal(followInfo.getUserId(), "2"); //用户关注数加1
            portalUserInfoMapper.updateFansTotal(followInfo.getFollowUserId(), "2"); //被关注的用户粉丝数加1
            PortalUserInfo userInfo = portalUserInfoMapper.findByUserId(followInfo.getFollowUserId());
            return userInfo.getFansTotal();
        }else{
            throw new Exception("插入关注记录失败！");
        }
    }

    /**
     * 根据关注者被关注者查询关注信息
     * @param userId
     * @param hisUserId
     * @return
     */
    @Override
    public FollowInfo findByUserIdAndFollowUserId(String userId, String hisUserId) {
        return followInfoMapper.findByUserIdAndFollowUserId(userId,hisUserId);
    }


    /**
     * @Author 肖拔赟
     * @Description 6.1	获取关注的达人列表接口
     * @Date Created on 2019-04-25 11:08
     * @param userId 我
     * @param currentPage 页码
     * @return
     */
    public List<FollowUserVo> getFollowListListPage (String userId, Integer currentPage){
        //List<FollowUserVo> list = portalUserInfoService.findFollowUserListByUserId(userId, page);
        PageData pageData = new PageData();
        pageData.put("userId", userId);
        pageData.put("userType", 3);//已关注用户
        Page page = new Page();
        page.setPd(pageData);
        page.setShowCount(Const.PORTAL_PAGE);
        page.setCurrentPage(currentPage);

        List<FollowUserVo> list = portalUserInfoMapper.findFollowUserListPage(page);
        for (int i =0; i< list.size() ; i++){
            FollowUserVo followUserVo = list.get(i);
            String tipContent = "";
            Integer todayTotal = articleInfoMapper.getWithinTodayArticleForUser(followUserVo.getUserId());
            if(todayTotal < 1){
                continue;
            }

            Map<String,Object> withinAnHourArticle = articleInfoMapper.getWithinAnHourArticleForUser(followUserVo.getUserId());
            if(null != withinAnHourArticle){
                tipContent = withinAnHourArticle.get("releaseTime").toString() + "分钟前更新了1条方案，";
            }
            tipContent = tipContent + "共"+ todayTotal +"条新方案";
            followUserVo.setTipContent(tipContent);
        }

        return list;
    }

    /**
     * @Author 肖拔赟
     * @Description 6.2	获取达人排行榜接口
     * @Date Created on 2019-04-26 10:06
     * @param userId userId
     * @param currentPage 页码
     * @return List<FollowUserVo>
     */
    public List<FollowUserVo> getFollowRankingListPage(String userId, Integer currentPage){
        PageData pageData = new PageData();
        pageData.put("userId", userId);
        Page page = new Page();
        page.setPd(pageData);
        page.setShowCount(Const.PORTAL_PAGE);
        page.setCurrentPage(currentPage);

        return followInfoMapper.getFollowRankingListPage(page);
    }
}
